package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.fragment.ActivityFragment;
import cst.neu.yuejian_31.fragment.ClubFragment;
import cst.neu.yuejian_31.fragment.PostFragment;
import cst.neu.yuejian_31.fragment.TrainFragment;

public class MainActivity extends BaseActivity {
    private TextView activity, club, train, post;
    private ImageView iv1,iv2,iv3,iv4;
    private ImageButton userButton,messageButton;
    private Context context;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initFragment();
    }

    private void initFragment() {
        listfragment =new ArrayList<Fragment>();

        listfragment.add(new ActivityFragment());
        listfragment.add(new ClubFragment());
        listfragment.add(new TrainFragment());
        listfragment.add(new PostFragment());

        fpadapter =new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp= (ViewPager) findViewById(R.id.viewPager);
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    View.OnClickListener onClickListener=new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            resetImage();
            switch (view.getId()){
                case R.id.textView_activity:
                    selectTab(0);
                    break;
                case R.id.textView_club:
                    selectTab(1);
                    break;
                case R.id.textView_train:
                    selectTab(2);
                    break;
                case R.id.textView_post:
                    selectTab(3);
                    break;
            }
        }
    };
    private void init(){
        iv1= (ImageView) findViewById(R.id.imageView_activity);
        iv2= (ImageView) findViewById(R.id.imageView_club);
        iv3= (ImageView) findViewById(R.id.imageView_train);
        iv4= (ImageView) findViewById(R.id.imageView_post);

        activity= (TextView) findViewById(R.id.textView_activity);
        club= (TextView) findViewById(R.id.textView_club);
        train= (TextView) findViewById(R.id.textView_train);
        post= (TextView) findViewById(R.id.textView_post);
        userButton= (ImageButton) findViewById(R.id.imageButton_user);
        messageButton= (ImageButton) findViewById(R.id.imageButton_message);


        activity.setOnClickListener(onClickListener);
        club.setOnClickListener(onClickListener);
        train.setOnClickListener(onClickListener);
        post.setOnClickListener(onClickListener);
        context=this;
        userButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context,UserActivity.class);
                intent.putExtra("user_id",getUserId());
                context.startActivity(intent);
            }
        });
        messageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,MessageActivity.class);
                intent.putExtra("user_id",getUserId());
                context.startActivity(intent);
            }
        });

    }
    private void resetImage(){
        iv1.setImageResource(R.drawable.ic_activity_uncheked);
        iv2.setImageResource(R.drawable.ic_club_uncheked);
        iv3.setImageResource(R.drawable.ic_train_uncheked);
        iv4.setImageResource(R.drawable.ic_post_uncheked);
    }
    private void selectTab(int i) {
        switch (i) {
            case 0:
                iv1.setImageResource(R.drawable.ic_activity);
                break;
            case 1:
                iv2.setImageResource(R.drawable.ic_club);
                break;
            case 2:
                iv3.setImageResource(R.drawable.ic_train);
                break;
            case 3:
                iv4.setImageResource(R.drawable.ic_post);
                break;

        }
        /**
         * 设置界面变化
         */
        vp.setCurrentItem(i);
    }
}